const util = require('../../utils/util.js')
var app = getApp();
var start_clientX;
var end_clientX;

Page({
  data: {
    windowWidth: wx.getSystemInfoSync().windowWidth,
    sections:[],
    knowledgeViews:{},
    cid:1,
    vid:1,
    sid:1,
    knowledgeViews: [
      {
        knowledgeSeq: 1,
        knowledgeSection: 1006,
        kid: 1006,
        knowledgeName: "### 本章概述",
        paragraphs: [
          {
            paragraphType: "P",
            paragraphSeq: 1120201001,
            paragraphKnowledge: 1006,
            paragraphContent: "本章概述加载中...",
            pid: "5de239e7475f436c9ad09d94"
          },
        ]
      }
    ]
  },
  onLoad:function(options){
    console.log(options);
    var vid=options.vid;
    var cid=options.cid;
    var self=this;
    
    // 获取知识见解目录
    wx.request({
      url: 'https://szsyph.cn/course/getCourseDetails',
      data:{
        "vid":vid,
        "cid":cid
      },
      success:(res)=>{
        let sections=self.data.sections;
        sections=res.data.data.courseView.sections;
        for(let i=0;i<sections.length;i++)
        {
          sections[i].open=false;
        }
        self.setData({sections});
        self.setData({"cid":cid});
        self.setData({"vid":vid});
        var sid=self.data.sections[0].sub[0].sid;
        self.setData({"sid":sid});
        console.log(self.data.knowledgeViews);

        //默认主页显示第一章
        wx.request({
         url: 'https://szsyph.cn/section/getSection',
        data:{
          "vid":self.data.vid,
          "cid":self.data.cid,
          "sid":self.data.sid
        },
        success:(res)=>{
          var self=this;
          let knowledgeViews=self.data.knowledgeViews;
          knowledgeViews=res.data.data.sectionView.knowledgeViews;
          self.setData({knowledgeViews});
          console.log(knowledgeViews);
        },
        fail:(res)=>{
          console.log("知识见解章节内容出错了");
        }
    })
      },
      fail:(res)=>{
        console.log("知识见解目录出错了");
      }
    });
  },
  touchend: function (e) {
    end_clientX = e.changedTouches[0].clientX;
    if (end_clientX - start_clientX > 120) {
      this.setData({
        display: "block",
        translate: 'transform: translateX(' + this.data.windowWidth * 0.7 + 'px);'
      })
    } else if (start_clientX - end_clientX > 100) {
      this.setData({
        display: "none",
        translate: ''
      })
    }
  },
 
  touchstart: function (e) {
    start_clientX = e.changedTouches[0].clientX;
  },
 
  hideview: function () {
    this.setData({
      display: "none",
      translate: '',
    })
    console.log("hideView");
  },
kindToggle(e) {
  const id = e.currentTarget.id
  const sections = this.data.sections
  for (let i = 0, len = sections.length; i < len; ++i) {
    if (sections[i].index == id) {
      sections[i].open = !sections[i].open
    } else {
      sections[i].open = false
    }
  }
  /**
   * key和value名称一样时，可以省略
   *
   * list:list=>list
   */
  this.setData({
    sections
  })
},
showDetail:function(e){
  var self=this;
  var sid=e.currentTarget.dataset.sid;
  self.setData({"sid":sid});
  wx.request({
    url: 'https://szsyph.cn/section/getSection',
    data:{
      "vid":self.data.vid,
      "cid":self.data.cid,
      "sid":sid
    },
    success:(res)=>{
      var self=this;
      let knowledgeViews=self.data.knowledgeViews;
      knowledgeViews=res.data.data.sectionView.knowledgeViews;
      self.setData({knowledgeViews});
    },
    fail:(res)=>{
      console.log("知识见解章节内容出错了");
    }
  })
} 



})